package com.vinjune.zookeeper.base;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;
import java.util.List;

/**
 * Created by 000376 on 2017/11/18.
 */
public class DeleteGroup extends ConnectionWatcher {

    public void delete(String groupName) throws InterruptedException, KeeperException {

        String path = "/" + groupName;
        List<String> children;
        try {
            children = zk.getChildren(path, false);
            for (String child : children) {
                //version:-1 表示不管版本号，直接删除
                zk.delete(path + "/" + child, -1);
            }
            zk.delete(path, -1);
        } catch (KeeperException.NoNodeException e) {
            System.out.printf("Group %s does not exist\n", groupName);
            System.exit(1);
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException, KeeperException {
        DeleteGroup deleteGroup = new DeleteGroup();
        deleteGroup.connect();
        deleteGroup.delete(args[1]);
        deleteGroup.close();
    }
}
